package com.mediacloud.app.newsmodule.fragment.video.vod;


import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.mediacloud.app.fbnavsk.SecondNav;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.newsmodule.adaptor.NewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.adaptor.bigimg_nosliceline.BigImageNoSliceLineAdaptor;
import com.mediacloud.app.newsmodule.adaptor.video.vod.VideoNewsListItemStyleAdaptor;
import com.mediacloud.app.newsmodule.fragment.newslist.NewsListWidthBannerFragment;
import com.mediacloud.app.newsmodule.utils.HistoryInvoker;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.StyleOther;
import com.mediacloud.app.user.model.UserInfo;

/**
 * 视频点播类的新闻
 */
public class VideoNewsListFragment extends NewsListWidthBannerFragment implements PlayClickListener {

    public VideoNewsListFragment() {
    }

    @Override
    public void initView() {
        super.initView();
        if (adaptor instanceof NewsListItemStyleAdaptor){
            ((NewsListItemStyleAdaptor) adaptor).destroy();
        }
        if (adaptor instanceof BigImageNoSliceLineAdaptor){
            ((BigImageNoSliceLineAdaptor) adaptor).destroy();
        }
        //如果有其他样式的 就用这个列表样式了
        if (catalogItem.getCatalogStyle() == StyleOther.OtherStyle)//&&catalogItem.getStyleOther()!=null)
        {
            adaptor = new BigImageNoSliceLineAdaptor(getActivity(), catalogItem,this);
        }else{
            adaptor = new VideoNewsListItemStyleAdaptor(getActivity(), catalogItem, this);
        }
        mCatalogContentItemListView.setAdapter(adaptor);
//		}
    }

    @Override
    public void onDestroyView() {
        if (adaptor instanceof VideoNewsListItemStyleAdaptor){
            ((VideoNewsListItemStyleAdaptor) adaptor).clearEvent();
            ((VideoNewsListItemStyleAdaptor) adaptor).clearSpiderObservers();
        }
        super.onDestroyView();
    }

    @Override
    protected void getNewsListData()
    {
        int extraStyle = catalogItem != null ? catalogItem.getCatalogStyle() : 0;
        if(extraStyle==2) {
            matchLBSCataglogId();
            if (dataInvoker != null && catalogID != null) {
                dataInvoker.getContentListWithAd(catalogID, pageNum, pageSize,null);

            }
        }
        else
        {
            super.getNewsListData();
        }
    }



    @Override
    protected void addBanner() {
        int extraStyle = catalogItem != null ? catalogItem.getCatalogStyle() : 0;
        if (extraStyle > 0)
            haveBanner = false;
        else
            super.addBanner();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);
        if (mVideoListPlayerUtil != null) {
            mVideoListPlayerUtil.stopPlay();
        }
    }

   public VideoListPlayerUtil mVideoListPlayerUtil;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mVideoListPlayerUtil != null)
            mVideoListPlayerUtil.onDestroy();
    }

     HistoryInvoker historyInvoker;
    @Override
    public void onPlayIcoClick(View rootView, ViewGroup container,View playIcon, final ArticleItem item) {
        if (historyInvoker == null) {
            historyInvoker = new HistoryInvoker();
        }
        //20200312新增需求，说是不用增加历史记录了，这段注释掉
//        if (UserInfo.getUserInfo(getContext()).isLogin()) {
//            AddHistoryParamsData paramsData = new AddHistoryParamsData();
//            paramsData.source = item.getType();
//            paramsData.source_id = item.getId();
//            paramsData.summary = item.getSummary();
//            paramsData.title = item.getTitle();
//            paramsData.type = ""+item.getType();
//            paramsData.url = item.getUrl();
//            paramsData.logo = item.getLogo();
//            historyInvoker.addHistory(UserInfo.getUserInfo(getContext()).getUserid(), paramsData, new HistoryInvoker.AddHistoryCallback() {
//                @Override
//                public void onSuccess() {
//                    LogUtil.e("添加成功");
//                }
//
//                @Override
//                public void onError() {
//
//                }
//            });
//        }

        if (mVideoListPlayerUtil == null) {
            mVideoListPlayerUtil = new VideoListPlayerUtil(getActivity(), mCatalogContentItemListView,this);
            mVideoListPlayerUtil.setHqyNavFragment(this);
            if(getParentFragment() instanceof  SecondNav){
                mVideoListPlayerUtil.mPlayer.setViewPlayer(((SecondNav)getParentFragment() ).getViewPager());
            }
        }

        if (item!=null&&mVideoListPlayerUtil.item!=null&&mVideoListPlayerUtil.getPlayer()!=null
                &&mVideoListPlayerUtil.item.equals(item)&&mVideoListPlayerUtil.getPlayer().isPlaying()){
            return;
        }
        if(getParentFragment() instanceof SecondNav){
            mVideoListPlayerUtil.mPlayer.setViewPlayer(((SecondNav)getParentFragment() ).getViewPager());
        }
        mVideoListPlayerUtil.setPlayerContainer(rootView,container,playIcon,!TextUtils.isEmpty(item.getGoodsID()),new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                item.setParentId(catalogID);
                NewsItemClickUtils.OpenItemNewsHandle(getActivity(), item.getType(), item, catalogItem);
            }
        },item);
        mVideoListPlayerUtil.setVideoDetail(catalogID + "", item);
    }

    boolean beforPauseIsNoLogin;
    @Override
    public void onPause() {
        super.onPause();

        beforPauseIsNoLogin=UserInfo.isLogin(getActivity());
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            if (mVideoListPlayerUtil != null) {
                mVideoListPlayerUtil.stopPlay();
            }
        }

    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(!isVisibleToUser){
            if (mVideoListPlayerUtil != null) {
                mVideoListPlayerUtil.stopPlay();
            }
        }
    }
}